var fs = require('fs');
var path = require('path');
var file_util = require('./public/file_util');
var inputFileName = process.argv[2];
var outputFileName = process.argv[3]
// var lang = process.argv[4];
// const Define = require('./src/ExportDefine');
function readFile(inputFileName, outputFileName) {
	// let export_path = path.join(export_dir, Define.LANG_PATH)
	// var inputFileName = path.join(export_path, moduleName + '_lang_zh.json')
	// var outputFileName = path.join(export_path, moduleName + '_lang_' + lang + '.json')
	//console.log('outputFileName', outputFileName)
	if (fs.existsSync(inputFileName)) {
		var fileString = file_util.readFile(inputFileName);
		var inputObj = JSON.parse(fileString)
		var exportObj = {}
		if (fs.existsSync(outputFileName)) {
			var exportString = file_util.readFile(outputFileName);
			exportObj = JSON.parse(exportString)
		}
		//console.log('exportObj', exportObj)
		copyContent(exportObj, inputObj)
		// let flieName = path.join(export_path, moduleName + '_lang_' + lang + '.json')
		var tempObj = {
			"isEncode": 0,
			"data": {
				"data": {

				},
				"type": 2,
				"name": "GameLangModel"
			}
		}
		var inputData = inputObj.data.data;
		var outData = exportObj.data.data
		for (const key in inputData) {
			if (Object.prototype.hasOwnProperty.call(inputData, key)) {
				const element = outData[key];
				tempObj.data.data[key] = element;
			}
		}
		console.log('tempObj', tempObj)
		file_util.writeFile(outputFileName, JSON.stringify(tempObj, null, 2));
	} else {
		console.log('not find ', inputFileName)
	}

}

function copyContent(outputObj, inputObj) {
	for (let key in inputObj) {
		let value = inputObj[key]
		//console.log('typeof value ',typeof value)
		if (typeof value == 'object') {
			if (!outputObj[key]) {
				outputObj[key] = {}
			}
			//console.log('copyContent[key] ', key, outputObj[key])
			copyContent(outputObj[key], value)
			delContent(outputObj[key], value)
		} else {
			//console.log('outputObj[key] ', key, outputObj[key])
			if (!outputObj[key]) {
				outputObj[key] = inputObj[key]
			}
		}
	}
}

function delContent(outputObj, inputObj) {
	for (let key in outputObj) {
		let value = outputObj[key]
		//console.log('typeof value ',typeof value)
		if (typeof value == 'object') {
			if (!inputObj[key]) {
				inputObj[key] = {}
			}
			delContent(value, inputObj[key])
		} else {
			//console.log('outputObj[key] ', key, outputObj[key])
			if (!inputObj[key]) {
				delete outputObj[key]
			}
		}
	}
}

readFile(inputFileName, outputFileName)
